package com.shunbo.convenient.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shunbo.convenient.service.ProductOrderService;
import com.shunbo.convenient.service.ProductService;
import com.shunbo.convenient.utils.DateUtils;
import com.shunbo.convenient.utils.ConvenientResult;
import com.shunbo.convenient.utils.JsonUtils;
import com.shunbo.convenient.vo.Product;
import com.shunbo.convenient.vo.ProductOrder;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author weizhuang
 * 商品
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private ProductService productService;
    @Resource
    private ProductOrderService productOrderService;

    /**
     * 商品列表
     *
     * @param product 商品vo
     * @return Result结果集
     */
    @RequestMapping("/list")
    public ConvenientResult getProductList(@RequestBody Product product) {
        try {
            List<Product> all = productService.findAll(product);
            for (Product pro : all
                    ) {
                Integer otherStock = 0;
                ProductOrder productOrder = new ProductOrder();
                productOrder.setPid(pro.getId());
                List<ProductOrder> all1 = productOrderService.findAll(productOrder);
                for (ProductOrder productOrders : all1
                        ) {
                    otherStock += productOrders.getNum();
                }
                int result = Integer.parseInt(pro.getStock()) - otherStock;
                pro.setOtherStock(result + "");
            }
            //按照type分组
            Map<String, List<Product>> collect = all.stream().collect(Collectors.groupingBy(Product::getType));
            return ConvenientResult.build(200, "成功", collect);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 添加商品(后管)
     *
     * @param product 商品vo
     * @return Result结果集
     */
    @RequestMapping("/saveManager")
    public ConvenientResult saveProductManager(@RequestBody Product product) {
        try {
            Integer save = productService.save(product);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }
    /**
     * 添加商品
     *
     * @param product 商品vo
     * @return Result结果集
     */
    @RequestMapping("/save")
    public ConvenientResult saveProduct(@RequestBody Product product) {
        try {
            System.out.println(JsonUtils.objectToJson(product));
            Product byId = productService.findById(product.getId()+"");
            System.out.println(JsonUtils.objectToJson(byId));
            //防止前端误带主键
            byId.setId(null);
            byId.setSid(product.getSid());
            byId.setMoney(product.getMoney());
            byId.setPrice(product.getPrice());
            byId.setStock(product.getStock());
            byId.setCreateTime(DateUtils.getDate());
            byId.setStockWarning(product.getStockWarning());
            byId.setStatus(product.getStatus());
            byId.setPid(product.getId()+"");
            Integer save = productService.save(byId);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 更新商品
     *
     * @param product 商品vo
     * @return Result结果集
     */
    @RequestMapping("/update")
    public ConvenientResult updateProduct(@RequestBody Product product) {
        try {
            Integer save = productService.update(product);
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 删除商品
     *
     * @param product 商品vo
     * @return Result结果集
     */
    @RequestMapping("/delete")
    public ConvenientResult deleteProduct(@RequestBody Product product) {
        try {
            Integer save = productService.delete(product.getId() + "");
            return ConvenientResult.build(200, "成功", save);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * id查商品
     *
     * @param product 商品vo
     * @return Result结果集
     */
    @RequestMapping("/findOne")
    public ConvenientResult getProductById(@RequestBody Product product) {
        try {
            Product prod = productService.findById(product.getId() + "");
            return ConvenientResult.build(200, "成功", prod);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 推荐商品
     *
     * @param product 商品id集合
     * @return Result结果集
     */
    @RequestMapping("/updateTop")
    public ConvenientResult updateProductTop(@RequestBody Product product) {
        try {
            String[] split = product.getIds().split(",");
            for (String str : split
                    ) {
                Product prod = new Product();
                prod.setId(Long.parseLong(str));
                prod.setIsTop("1");
                productService.update(prod);
            }
            return ConvenientResult.build(200, "成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 商品列表(不分类)
     *
     * @param product 商品vo
     * @return Result结果集
     */
    @RequestMapping("/listType")
    public ConvenientResult getProductListType(@RequestBody Product product) {
        try {
            if (product.getPageNo() == null) {
                product.setPageNo(1);
            }
            PageHelper.startPage(product.getPageNo(), 10);
            List<Product> all = productService.findAll(product);
            for (Product prod : all
                    ) {
                ProductOrder productOrder = new ProductOrder();
                Integer otherStock = 0;
                productOrder.setPid(prod.getId());
                List<ProductOrder> all1 = productOrderService.findAll(productOrder);
                for (ProductOrder productOrders : all1
                        ) {
                    otherStock += productOrders.getNum();
                }
                int result = Integer.parseInt(prod.getStock()) - otherStock;
                prod.setOtherStock(result + "");
                prod.setVolume(otherStock);
            }
            PageInfo<Product> pageInfo = new PageInfo<>(all);
            Map<String, Object> map = new HashMap<>();
            map.put("productList", all);
            map.put("pageNo", product.getPageNo());
            map.put("count", pageInfo.getTotal());
            return ConvenientResult.build(200, "成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 商品列表名字模糊查询(不分类)
     *
     * @param product 商品vo
     * @return Result结果集
     */
    @RequestMapping("/listName")
    public ConvenientResult listName(@RequestBody Product product) {
        try {
            if (product.getPageNo() == null) {
                product.setPageNo(1);
            }
            if (StringUtils.isEmpty(product.getName())) {
                return ConvenientResult.build(400, "参数不能为空", false);
            }
            PageHelper.startPage(product.getPageNo(), 10);
            List<Product> all = productService.findByName(product);
            for (Product prod : all
                    ) {
                ProductOrder productOrder = new ProductOrder();
                productOrder.setPid(prod.getId());
                Integer otherStock = 0;
                List<ProductOrder> all1 = productOrderService.findAll(productOrder);
                for (ProductOrder productOrders : all1
                        ) {
                    otherStock += productOrders.getNum();
                }
                int result = Integer.parseInt(prod.getStock()) - otherStock;
                prod.setOtherStock(result + "");
                prod.setVolume(otherStock);
            }
            PageInfo<Product> pageInfo = new PageInfo<>(all);
            Map<String, Object> map = new HashMap<>();
            map.put("productList", all);
            map.put("pageNo", product.getPageNo());
            map.put("count", pageInfo.getTotal());
            return ConvenientResult.build(200, "成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }

    /**
     * 商品分类
     *
     * @return Result结果集
     */
    @RequestMapping("/typeList")
    public ConvenientResult typeList(@RequestBody Product product) {
        try {
            Set<String> type = new HashSet<>();
            List<Product> all = productService.findAll(product);
            for (Product prod : all
                    ) {
                type.add(prod.getType());
            }
            return ConvenientResult.build(200, "成功", type);
        } catch (Exception e) {
            e.printStackTrace();
            return ConvenientResult.build(500, "系统错误", e.getStackTrace());
        }
    }


}
